package ru.jkff.jalgebra.monoid;

import static ru.jkff.jalgebra.utils.CollectionFactory.newUnorderedMap;

import java.util.Map;
import java.util.Set;

/**
 * User: jkff
 * Date: Aug 22, 2009
 * Time: 12:59:54 PM
 */
public final class MonoidMap<K,V> {
    private Monoid<V> valueMonoid;
    private Map<K,V> underlyingMap = newUnorderedMap();

    public MonoidMap(Monoid<V> valueMonoid) {
        this.valueMonoid = valueMonoid;
    }

    public V get(K key) {
        if(underlyingMap.containsKey(key)) {
            return underlyingMap.get(key);
        }
        return valueMonoid.unit();
    }

    public void pushFront(K key, V value) {
        underlyingMap.put(key, valueMonoid.combine(value, get(key)));
    }
    public void pushBack(K key, V value) {
        underlyingMap.put(key, valueMonoid.combine(get(key), value));
    }

    public Set<K> explicitKeySet() {
        return underlyingMap.keySet();
    }
}
